<?php

namespace App\Http\Controllers\Admin;
use App\DTOS\Admin\UserRoleDTO;

use App\Http\Controllers\Controller;
use App\Services\Game\UserMemberService;
use App\Services\Game\UserRoleService;
use App\Utils\ResponseUtil;
use Illuminate\Http\Request;


class RoleCreateController extends Controller
{

    private Request $request;
    private UserRoleService $userRoleService;
    private UserMemberService $userMemberService;

    /**
     * RoleCreateController constructor.
     * @param Request $request
     * @param UserRoleService $userRoleService
     * @param UserMemberService $userMemberService
     */
    public function __construct(Request $request, UserRoleService $userRoleService, UserMemberService $userMemberService)
    {
        $this->request = $request;
        $this->userRoleService = $userRoleService;
        $this->userMemberService = $userMemberService;
    }


    private function buildRes()
    {
        global $gameArea;
        $searchData = $this->request->all();
        $username = data_get($searchData,'username');
        $roleName = data_get($searchData,'nick');
        $gameRoleTDO = new UserRoleDTO(['limit'=>9999999]);
        $searchedRoleTable = false;
        if ($username){
            $searchedRoleTable = true;//间接搜索了角色表
            $gameRoleTDO->setUsername($username);
        }
        if ($roleName){
            $searchedRoleTable = true;
            $gameRoleTDO->setNick($roleName);
        }
        if ($searchedRoleTable){ //最后搜索角色表
            $gameRoleTDO->setFields(['id','memid']);
            $userRolePaginate = $this->userRoleService->paginate($gameRoleTDO);
            if (empty($userRolePaginate->items())){
                throw new \Exception("没有数据");
            }
            $roleIds = collect($userRolePaginate->items())->pluck('id')->toArray();

            if (empty($roleIds)) {
                throw new \Exception("没有数据");
            }
            $searchData['roleIds'] = $roleIds;
        }
        $res = callRpc("/RoleCreateService/paginate",$searchData,['gameArea'=>$gameArea]);
        if (empty($res['data'])){
            throw new \Exception("没有数据");
        }
        return $res;
    }

    public function paginate(){

        try {
            $res = $this->buildRes();
            return ResponseUtil::json($res);
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }

    public function export()
    {
        try {
            $res = $this->buildRes();
            $excelRows = [
                ['记录时间','服务器ID','游戏账号','角色ID','角色名','角色姓别','角色职业']
            ];
            foreach($res['data'] as $datum){
                $excelRows[] = [
                    $datum['logTime'] ?? null,
                    $datum['memberAreaId'] ?? null,
                    $datum['memberName'] ?? null,
                    $datum['roleId'] ?? null,
                    $datum['nick'] ?? null,
                    $datum['sex'] ?? null,
                    $datum['jobName'] ?? null,
                ];
            }
            return ResponseUtil::success(cacheDownload(arrayToScvContent($excelRows),'.角色创建日志.csv'));
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }

    }

}
